"use client";

import { useEffect, useState } from "react";
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";

dayjs.extend(relativeTime);

export const AutoUpdatedDate = ({ date }: { date: string }) => {
  const [currentDate, setCurrentDate] = useState(dayjs(date).fromNow());

  useEffect(() => {
    const intervalId = setInterval(() => {
      setCurrentDate(dayjs(date).fromNow());
    }, 1000);

    return () => clearInterval(intervalId);
  }, []);

  return <span className="text-lg">{currentDate}</span>;
};
